import 'package:boss/ui/page/role/page/roleDetail/provider/role_detail_provider.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/radioList/radio.dart' as radio;
import 'package:boss/ui/widget/vertical_tab_view.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

@RouterConfig(RolePermCon.pageName)
class RolePermCon extends StatefulWidget {
  static const pageName = '/RolePermCon';

  @Autowired()
  final int roleId;

  RolePermCon(this.roleId);

  @override
  _RolePermConState createState() => _RolePermConState();
}

class _RolePermConState extends BaseState<RolePermCon> {
  _RolePermConState() : super(RolePermCon.pageName);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(title: '选择权限'),
      body: Consumer<RoleDetailProvider>(
        builder: (context, provider, _) {
          return Column(
            children: [
              Expanded(
                child: VerticalTabView(
                  tabs: provider.tabs,
                  position: provider.tabPosition,
                  tabHeight: 90.w,
                  onTabTap: provider.setTabPosition,
                  builder: (context) {
                    return ListView.builder(
                      padding: EdgeInsets.symmetric(horizontal: 20.w),
                      itemBuilder: (context, index) {
                        if (index == 0) {
                          final isSelectedAll = provider.contentPerms.every((element) => element.permissionSelect);
                          return CustomListTitle(
                            title: '全选',
                            trailing: radio.Radio(isSelected: isSelectedAll),
                            onTap: () => provider.togglePerms(!isSelectedAll),
                          );
                        }
                        final current = provider.contentPerms[index - 1];
                        return CustomListTitle(
                          title: current.permissionName,
                          trailing: radio.Radio(isSelected: current.permissionSelect),
                          onTap: () => provider.togglePerm(index - 1),
                        );
                      },
                      itemCount: provider.contentPerms.length + 1,
                    );
                  },
                ),
              ),
              FixedBottomBtn(
                title: '确定',
                onTap: () {
                  Navigator.pop(context);
                },
              ),
            ],
          );
        },
      ),
    );
  }
}